import router from '@/router'
import {
    cloneDeep
} from 'lodash'
import {
    database as getDatabase,
    dbGet,
    dbSet
} from '@/utils/store/db'

export default {
    namespaced: true,
    actions: {
        /**
         * @description 将数据存储到指定位置 | 路径不存在会自动初始化
         * @description 效果类似于取值 dbName.path = value
         * @param {Object} context
         * @param {Object} payload dbName {String} 数据库名称
         * @param {Object} payload path {String} 存储路径
         * @param {Object} payload value {*} 需要存储的值
         * @param {Object} payload user {Boolean} 是否区分用户
         */
        set(context: any, {
            dbName = 'database',
            path = '',
            value = '',
            user = false
        }: any) {
            dbSet({
                dbName,
                path,
                value,
                user
            })
        },
        /**
         * @description 获取数据
         * @description 效果类似于取值 dbName.path || defaultValue
         * @param {Object} context
         * @param {Object} payload dbName {String} 数据库名称
         * @param {Object} payload path {String} 存储路径
         * @param {Object} payload defaultValue {*} 取值失败的默认值
         * @param {Object} payload user {Boolean} 是否区分用户
         */
        get(context: any, {
            dbName = 'database',
            path = '',
            defaultValue = '',
            user = false
        }: any) {
            return dbGet({
                dbName,
                path,
                defaultValue,
                user
            })
        },
        /**
         * @description 获取存储数据库对象
         * @param {Object} context
         * @param {Object} payload user {Boolean} 是否区分用户
         */
        database(context: any, {
            user = false
        }: any = {}) {
            return getDatabase({
                user,
                defaultValue: {}
            })
        },
        /**
         * @description 清空存储数据库对象
         * @param {Object} context
         * @param {Object} payload user {Boolean} 是否区分用户
         */
        databaseClear(context: any, {
            user = false
        }: any = {}) {
            return getDatabase({
                user,
                validator: () => false,
                defaultValue: {}
            })
        },
        /**
         * @description 获取存储数据库对象 [ 区分页面 ]
         * @param {Object} context
         * @param {Object} payload basis {String} 页面区分依据 [ name | path | fullPath ]
         * @param {Object} payload user {Boolean} 是否区分用户
         */
        databasePage(context: any, {
            basis = 'fullPath',
            user = false
        }: any = {}) {
            return getDatabase({
                path: `$page.${(router as any).app.$route[basis]}`,
                user,
                defaultValue: {}
            })
        },
        /**
         * @description 清空存储数据库对象 [ 区分页面 ]
         * @param {Object} context
         * @param {Object} payload basis {String} 页面区分依据 [ name | path | fullPath ]
         * @param {Object} payload user {Boolean} 是否区分用户
         */
        databasePageClear(context: any, {
            basis = 'fullPath',
            user = false
        }: any = {}) {
            return getDatabase({
                path: `$page.${(router as any).app.$route[basis]}`,
                user,
                validator: () => false,
                defaultValue: {}
            })
        },
        /**
         * @description 快速将页面当前的数据 ( $data ) 持久化
         * @param {Object} context
         * @param {Object} payload instance {Object} vue 实例
         * @param {Object} payload basis {String} 页面区分依据 [ name | path | fullPath ]
         * @param {Object} payload user {Boolean} 是否区分用户
         */
        pageSet(context: any, {
            instance,
            basis = 'fullPath',
            user = false
        }: any) {
            return getDatabase({
                path: `$page.${(router as any).app.$route[basis]}.$data`,
                user,
                validator: () => false,
                defaultValue: cloneDeep(instance.$data)
            })
        },
        /**
         * @description 快速获取页面快速持久化的数据
         * @param {Object} context
         * @param {Object} payload instance {Object} vue 实例
         * @param {Object} payload basis {String} 页面区分依据 [ name | path | fullPath ]
         * @param {Object} payload user {Boolean} 是否区分用户
         */
        pageGet(context: any, {
            instance,
            basis = 'fullPath',
            user = false
        }: any) {
            return dbGet({
                path: `$page.${(router as any).app.$route[basis]}.$data`,
                user,
                defaultValue: cloneDeep(instance.$data)
            })
        },
        /**
         * @description 清空页面快照
         * @param {Object} context
         * @param {Object} payload basis {String} 页面区分依据 [ name | path | fullPath ]
         * @param {Object} payload user {Boolean} 是否区分用户
         */
        pageClear(context: any, {
            basis = 'fullPath',
            user = false
        }: any) {
            return getDatabase({
                path: `$page.${(router as any).app.$route[basis]}.$data`,
                user,
                validator: () => false,
                defaultValue: {}
            })
        },
        /**
         * @description 根据数据主键快速将页面当前的指定数据持久化
         * @param {Object} context
         * @param {Object} payload instance {Object} vue 实例
         * @param {Object} payload basis {String} 页面区分依据 [ name | path | fullPath ]
         * @param {Object} payload id {String} 数据主键
         * @param {Object} payload user {Boolean} 是否区分用户
         */
        pageSetById(context: any, {
            data,
            basis = 'name',
            id = '',
            user = false
        }: any) {
            return getDatabase({
                // 直接.${id}数据库会创建一些无用的数据
                path: `$page.${(router as any).app.$route[basis]}${id}`,
                user,
                validator: () => false,
                defaultValue: cloneDeep(data)
            })
        },
        /**
         * @description 根据数据主键快速获取页面快速持久化的数据
         * @param {Object} context
         * @param {Object} payload instance {Object} vue 实例
         * @param {Object} payload basis {String} 页面区分依据 [ name | path | fullPath ]
         * @param {Object} payload id {String} 数据主键
         * @param {Object} payload user {Boolean} 是否区分用户
         */
        pageGetById(context: any, {
            data = {},
            basis = 'name',
            id = '',
            user = false
        }: any) {
            // console.log('pageGet', `$page.${(router as any).app.$route[basis]}${id}`);
            return dbGet({
                path: `$page.${(router as any).app.$route[basis]}${id}`,
                user,
                defaultValue: data
            })
        },
        /**
         * @description 根据数据主键清空页面快照
         * @param {Object} context
         * @param {Object} payload basis {String} 页面区分依据 [ name | path | fullPath ]
         * @param {Object} payload id {String} 数据主键
         * @param {Object} payload user {Boolean} 是否区分用户
         */
        pageClearById(context: any, {
            name = '',
            id = '',
            user = false
        }: any) {
            return getDatabase({
                path: `$page.${name}${id}`,
                user,
                validator: () => false,
                defaultValue: {}
            })
        }
    }
}